package org.ndot.java.util.concurrent.bank;

import java.util.List;

/**
 * @公司名称: 小蚂蚁学堂
 * 
 * @项目名称: temp
 * 
 * @类名名称: org.ndot.bank.Operator.java
 * 
 * @作者姓名: SunJincheng
 * 
 * @软件版本: v1.0
 * 
 * @创建日期: 2010-4-13下午04:04:55
 */
public class Operator extends Thread {
	private boolean end = false;

	private List<Consumer>[] consumerList;

	public Operator(List<Consumer>... consumerList) {
		this.consumerList = consumerList;
	}

	public void run() {
		try {
			while (!end || consumerList[0].size() > 0
					|| (consumerList.length > 1 && consumerList[1].size() > 0)) {
				synchronized (consumerList[0]) {
					if (consumerList[0].size() > 0) {
						Consumer c = consumerList[0].get(0);
						doBusiness(c);
						consumerList[0].remove(c);
					}
				}
				if (consumerList[0].size() == 0 && consumerList.length > 1) {
					synchronized (consumerList[1]) {
						if (consumerList[1].size() > 0) {
							Consumer c = consumerList[1].get(0);
							doBusiness(c);
							consumerList[1].remove(c);
						}
					}
				}
				Thread.sleep(1);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void setEnd(boolean end) {
		this.end = end;
	}

	private void doBusiness(Consumer cnosumer) {
		// System.out.println(Thread.currentThread().getName() + "-->开始处理第"
		// + cnosumer.getId() + "个"
		// + BankParam.printInfo(cnosumer.getType()) + "类型客户业务....");
		try {
			Thread.sleep(cnosumer.getTime() * 100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName() + "-->已经完成 第"
				+ cnosumer.getId() + "个"
				+ BankParam.printInfo(cnosumer.getType()) + "类型客户业务处理...");
	}
}
